#include "../include/Logger.hpp"
#include <iostream>
#include <chrono>
#include <vector>
#include <string>

// 定义要写入的日志数量
const int LOG_COUNT = 10000000;

void run_benchmark(senlog::Logger::ptr logger, const std::string& logger_type) {
    std::cout << "--- Starting Benchmark for: " << logger_type << " ---" << std::endl;
    
    auto start_time = std::chrono::high_resolution_clock::now();

    for (int i = 0; i < LOG_COUNT; ++i) {
        // 使用宏来模拟真实调用
        LOG_INFO(logger, "This is log message number %d, with a string payload: %s", i, "payload_string");
    }
    
    // 对于异步日志，需要等待其消费完所有日志
    // 在实际应用中，析构函数会自动处理，这里为了精确计时，我们手动等待
    if (logger_type.find("Async") != std::string::npos) {
         std::this_thread::sleep_for(std::chrono::seconds(2)); // 等待队列消费
    }


    auto end_time = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end_time - start_time;

    std::cout << "Logged " << LOG_COUNT << " messages." << std::endl;
    std::cout << "Total time elapsed: " << elapsed.count() << " seconds." << std::endl;
    std::cout << "Logs per second: " << (LOG_COUNT / elapsed.count()) << std::endl;
    std::cout << "-------------------------------------------------" << std::endl << std::endl;
}

int main() {
    // 1. 创建同步日志器并测试
    std::cerr << "Main function started." << std::endl;

    senlog::LocalLoggerBuilder sync_builder;
    sync_builder.buildLoggerName("sync_logger")
                .buildLoggerType(senlog::LoggerType::SYNC_LOGGER)
                .addSink<senlog::FileLogSink>("./logs/sync_baseline.log");
    auto sync_logger = sync_builder.build();
    run_benchmark(sync_logger, "Sync Logger (Baseline)");

    // 2. 创建异步日志器并测试
    senlog::LocalLoggerBuilder async_builder;
    async_builder.buildLoggerName("async_logger_unsafe")
                 .buildEnableUnsafe()
                 .buildLoggerType(senlog::LoggerType::ASYNC_LOGGER)
                 .addSink<senlog::FileLogSink>("./logs/async_baseline.log");
    auto async_logger = async_builder.build();
    run_benchmark(async_logger, "Async Logger Unsafe(Baseline)");
    
    return 0;
}